// 将x减到0的最小操作数
class Solution {
    public:
        int minOperations(vector<int>& nums, int x) {
            int right = 0;
            int n = nums.size();
            int sum = 0;
            int len = INT_MAX;
            for(auto e : nums)
            {
                sum += e;
            }
            if(sum < x) return -1;
            if(nums[0] > x && nums[n-1] > x) return -1;
            int lsum = 0,rsum = sum;
            for(int left = -1; left < right; left++)
            {
                if(left >= 0)
                {
                    lsum += nums[left];
                }
                while(right < n && lsum + rsum > x)
                {
                    rsum -= nums[right++];
                }
                if(lsum + rsum == x)
                {
                    len = min(len,left + 1 + n - right);
                }
            }
            return len == INT_MAX ? -1 : len;
        }
    };